
class Store{
    constructor(reducer,initialState){
        this._state=initialState
        this.reducer=reducer
        this._listeners=[]
    }
    getState(){
        return this._state
    }
    // 订阅使用了观察者模式
    subscribe(listener){
         this._listeners.push(listener)
         return ()=>{
            let position=this._listeners.indexOf(listener)
            this._listeners.splice(position)
         }
        //  return()=>{
        //      this._listeners=this._listeners.filter((l)=>{
        //        return l!=listener
        //      })
        //  }
    }
    dispatch(action){
        this._state=this.reducer(this._state,action)
        this._listeners.forEach((listener)=>{
            listener()
        })
      }
}

